package tech.yozo.bamboo.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;

import tech.yozo.bamboo.model.DictWord;
import tech.yozo.bamboo.utils.Utils;

public class DictDbHelper extends SQLiteOpenHelper {
    // If you change the database schema, you must increment the database version.
    public static final int DB_VERSION = 1;
    public static final String DB_NAME = "dict.db";
    public Context context;

    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + DictEntry.TABLE_NAME + " (" +
                    DictEntry._ID + " INTEGER PRIMARY KEY," +
                    DictEntry.COLUMN_WORD + " VARCHAR(64)," +
                    DictEntry.COLUMN_PHONETIC + " VARCHAR(64)," +
                    DictEntry.COLUMN_TRANSLATION + " TEXT," +
                    DictEntry.COLUMN_TAG + " VARCHAR(64)," +
                    DictEntry.COLUMN_EXCHANGE + " TEXT)";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + DictEntry.TABLE_NAME;

    public DictDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.context = context;
        File dbFile = context.getDatabasePath(DB_NAME);
        if (!dbFile.exists()) {
            Utils.copyFileFromAssets(context, "db/" + DB_NAME, dbFile.getPath());
        }
    }

    public void onCreate(SQLiteDatabase db) {
//        db.execSQL(SQL_CREATE_ENTRIES);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // This database is only a cache for online data, so its upgrade policy is
        // to simply to discard the data and start over
//        db.execSQL(SQL_DELETE_ENTRIES);
//        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        onUpgrade(db, oldVersion, newVersion);
    }

    public DictWord getDictWord(String word) {
        SQLiteDatabase db = getReadableDatabase();

        String selection = DictEntry.COLUMN_WORD + " = ?";
        String[] selectionArgs = {word};

        Cursor cursor = db.query(
                DictEntry.TABLE_NAME,   // The table to query
                null,                   // The array of columns to return (pass null to get all)
                selection,              // The columns for the WHERE clause
                selectionArgs,          // The values for the WHERE clause
                null,                   // don't group the rows
                null,                   // don't filter by row groups
                null                    // The sort order
        );

        DictWord dictWord = new DictWord();
        if(cursor.moveToNext()) {
            dictWord.word = cursor.getString(cursor.getColumnIndexOrThrow(DictEntry.COLUMN_WORD));
            dictWord.phonetic = cursor.getString(cursor.getColumnIndexOrThrow(DictEntry.COLUMN_PHONETIC));
            dictWord.translation = cursor.getString(cursor.getColumnIndexOrThrow(DictEntry.COLUMN_TRANSLATION));
            dictWord.tag = cursor.getString(cursor.getColumnIndexOrThrow(DictEntry.COLUMN_TAG));
            dictWord.exchange = cursor.getString(cursor.getColumnIndexOrThrow(DictEntry.COLUMN_EXCHANGE));
        }
        cursor.close();
        db.close();
        return dictWord;
    }
}